/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.dabarobjects.mobile.ui.models;

import com.dabarobjects.mobile.calls.instructions.CallQueryResponseFormatter;
import com.dabarobjects.mobile.calls.instructions.CallStatusFlags;
import com.dabarobjects.mobile.flow.CallStatusListener;
import com.dabarobjects.mobile.flow.notifiers.FeedbackMessageNotifier;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author DabarObjects Solutions (www.dabarobjects.com)
 */
public class CallStatusModel extends AbstractTableModel implements CallStatusListener, FeedbackMessageNotifier {

    private List<String> statusList;
    private boolean recordingOn;

    public CallStatusModel() {

        statusList = new ArrayList<String>();
    }

    public void addStatus(String st) {
        statusList.add(st);
        fireTableDataChanged();
    }

    public int getRowCount() {
        return statusList.size();
    }

    public int getColumnCount() {
        return 1;
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        String bean = statusList.get(rowIndex);
        return bean;
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        return String.class;
    }

    @Override
    public String getColumnName(int columnIndex) {

        return "Service Status";
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return false;
    }
    private boolean calledInActive;
    private boolean calledActive;
    private boolean calledIncoming;

    public void onCallQuery(CallQueryResponseFormatter format) {
        CallStatusFlags flag = format.getStatusId();
        switch (flag) {
            case INCOMING:
                if (calledIncoming) {
                    return;
                }
                calledIncoming = true;
                onCallIncoming(format.getActiveCallId());
                break;
            case ACTIVE:
                if (calledActive) {
                    return;
                }
                calledActive = true;
                onCallActive(format.getActiveCallId());
                break;
            case INACTIVE:
                if (calledActive) {
                    calledInActive = false;
                    calledIncoming = false;
                    calledActive = false;
                }
                if (calledInActive) {
                    return;
                }
                calledInActive = true;
                onCallInActive();
                break;
            default:
        }
    }

    public void onCallActive(String callId) {
        addStatus("Active Call With " + callId);
    }

    public void onCallIncoming(String callId) {
        addStatus("Incoming Call From " + callId);
        recordingOn = true;
    }

    public void onCallInActive() {
        if (recordingOn) {
            recordingOn = false;
            addStatus("Call Recorded");
        } else {
            addStatus("No Active Calls");
        }
    }

    public void onMessage(String message, Date time) {
        addStatus(message + " at " + time);
    }
}
